<template>
	<view class="goshop-recharge-container">
		<u-toast ref="uToast"></u-toast>
		<view class="goshop-recharge-type">
			<view class="goshop-recharge-type-left">{{typeText}}</view>
			<view class="goshop-recharge-type-right" @tap="show = true">
				<text>{{payMethod}}</text>
				<view class="iconfont iconfont-right_arrow"></view>
			</view>
		</view>
		<view class="goshop-recharge-money">
			<view class="goshop-recharge-money-head">{{balanceText}}</view>
			<view class="goshop-recharge-money-footer">
				<view class="goshop-recharge-money-footer-left">{{moneyText}}</view>
				<view class="goshop-recharge-money-footer-right">
					<u--input
					    :placeholder="fillbalanceText"
					    border="none"
						fontSize="20"
					    v-model="form.money"
					  ></u--input>
				</view>
			</view>
		</view>
		<view class="goshop-recharge-submit">
			<u-button type="error" @click="submit()" :text="confirmText"></u-button>
		</view>
		<u-action-sheet :actions="actions" @close="selectPayClose" @select="selectPayClick" :closeOnClickOverlay="true" :closeOnClickAction="true"  :title="title" :show="show"></u-action-sheet>
	</view>
</template>

<script>
	import {walletRecharge} from '@/common/http.api.js'
	import config from '@/common/config.js'
	export default {
		data() {
			return {
				form:{
					money:'',
					type:4,
					payment:'4',
				},
				callalipayText:'',
				alipayerrText:'',
				callwxText:'',
				wxerrText:'',
				callpaypalText:'',
				paypalerrText:'',
				typeText:'',
				balanceText:'',
				fillbalanceText:'',
				confirmText:'',
				payMethod:'',
				show:false,
				title:'',
				paySuccessText:'',
				payFailText:'',
				moneyText:'',
				actions:[
					{
						name: '',
						type:4
					}
				]
			};
		},
		onShow(){
			this.refresh();
		},
		methods:{
			refresh(){
				this.payMethod =  this.$t('common.paypalpay.text');
				this.actions[0].name = this.$t('common.paypalpay.text');
				this.moneyText = this.$t('common.money.text');
				this.callalipayText = this.$t('common.callalipay.text');
				this.alipayerrText = this.$t('common.alipayerr.text');
				this.callwxText = this.$t('common.callwx.text');
				this.wxerrText = this.$t('common.wxerr.text');
				this.callpaypalText = this.$t('common.callpaypal.text');
				this.paypalerrText = this.$t('common.paypalerr.text');
				this.typeText = this.$t('recharge.type.text');
				this.balanceText = this.$t('recharge.balance.text');
				this.fillbalanceText = this.$t('recharge.fillbalance.text');
				this.confirmText = this.$t('recharge.confirm.text');
				this.title = this.$t('recharge.selectmethod.text');
				
				this.paySuccessText = this.$t('common.pay.success');
				this.payFailText = this.$t('common.pay.fail');
			},
			selectPayClick(index){
				this.form.type = index.type;
				this.payMethod = index.name;
			},
			selectPayClose(){
			    this.show = false;
			},
			submit(){
				let that = this;
				walletRecharge(this.form).then(res => {
					if (res.code === 0) {
						that.payData = res.data.invokeResponse;
						console.info('支付响应：' + JSON.stringify(that.payData));
						debugger
							uni.getProvider({  //获取可用的支付环境
								service: 'payment',
								success: res=>{
									// #ifdef H5
									//支付宝
									if('2' == that.form.payment){

									}else if('3' == that.form.payment){
		
									}else if('4' == that.form.payment){
										window.location.href = that.payData.approve; //H5端跳转到paypal支付页面
									}
									// #endif

									// #ifdef APP-PLUS
										//支付宝
										if('2' == that.form.payment){
											//先判断用户是否有支付宝支付环境
											if (~res.provider.indexOf('alipay')) { 
												uni.showLoading({title: that.callalipayText})
											}else{
												uni.showToast({title: that.alipayerrText,icon:'none'})
												return;
											}
											//沙箱支付
											var EnvUtils = plus.android.importClass("com.alipay.sdk.app.EnvUtils");
											EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
											that.payData = that.payData.invokeResponse.body;
										}else if('3' == that.form.payment){
											 //先判断用户是否有微信支付环境
											if (~res.provider.indexOf('wxpay')) {
												uni.showLoading({title: that.callwxText})
											}else{
												uni.showToast({title:that.wxerrText,icon:'none'})
												return;
											}
										}else if('4' == that.form.payment){
											 //先判断用户是否有paypal支付环境
											if (~res.provider.indexOf('paypal')) {
												uni.showLoading({title: that.callpaypalText})
											}else{
												uni.showToast({title: that.paypalerrText,icon:'none'})
												return;
											}
											let orderInfo = {
											  "clientId": that.payData.clientId, //开发者中心clientId
											  "orderId": that.payData.orderId, //订单id
											  "userAction": "continue", //按钮样式 paynow/continue
											  "currency":"USD", //币种
											  "channelId":that.payData.channelId,
											  "environment":that.payData.mode //运行环境 sandbox(沙盒) / live(线上)
											};
											debugger
											that.payData = orderInfo;
											console.info('paypal app支付数据：' + that.payData);
										}
										console.info('支付类型：' + that.form.payment);
										console.info("支付方式：" + res.provider);
										console.info('支付数据：' + JSON.stringify(that.payData));
										debugger
										// 发起支付
										uni.requestPayment({
										  provider: 'paypal',
										  orderInfo: that.payData, //微信、支付宝订单数据
										  success: (res) => {
											   console.log('success:' + JSON.parse(res.rawdata));
											   uni.hideLoading(); 
											   this.$refs.uToast.show({
													type: 'success',
													message: that.paySuccessText,
													complete() {
													  uni.redirectTo({
														url: 'pages/common/hint/pay_success?type=order&status=success&id='+that.payData.orderId+'&channelId='+that.payData.channelId
													  });
													}
												})
										  },
										  fail: (res) => {
												uni.hideLoading(); 
												// 支付失败
												// uni.showModal({
												// 	content: "支付失败",
												// 	showCancel: false
												// })
												uni.showModal({
												  content: res.errMsg,
												  showCancel: false
												})
										  },
										})
									// #endif
								}
							});
					} else {
						uni.hideLoading();
						this.$refs.uToast.show({
							message: res.msg,
							type: 'error',
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.goshop-recharge-container{
	padding: 20rpx;
	.goshop-recharge-type{
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 0;
		border-bottom: 2rpx solid $goshop-text-color-four;
		.goshop-recharge-type-left{
			display: flex;
			align-items: center;
		}
		.goshop-recharge-type-right{
			display: flex;
			align-items: center;
		}
	}
	.goshop-recharge-money{
		display: flex;
		flex-direction: column;
		border-bottom: 2rpx solid $goshop-text-color-four;
		margin-top: 20rpx;
		.goshop-recharge-money-footer{
			display: flex;
			padding: 20rpx 0;
			font-size: 20px;
			.goshop-recharge-money-footer-left{
				
			}
			.goshop-recharge-money-footer-right{
				font-size: 20px;
				align-items: center;
			}
		}
	}
	.goshop-recharge-submit{
		margin-top: 100rpx;
	}
}
</style>
